﻿using MyKTV;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace HolidaysDemo1
{
    public partial class Form2 : Form
    {
        DBHelper dBHelper = new DBHelper();
        DataSet ds = new DataSet();
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            dBHelper.Openconnection();
            string sql = "select * from TeamInfo";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, dBHelper.Connection);
            dataAdapter.Fill(ds, "TeamInfo");

            comboBox1.DataSource = ds.Tables["TeamInfo"].Copy();
            comboBox1.ValueMember = "id";
            comboBox1.DisplayMember = "teamName";


            comboBox2.DataSource = ds.Tables["TeamInfo"].Copy();
            comboBox2.ValueMember = "id";
            comboBox2.DisplayMember = "teamName";

            dBHelper.Closeconnection();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text.Equals(string.Empty)&& this.textBox1.Text.Equals(string.Empty) && this.textBox1.Text.Equals(string.Empty))
            {
                MessageBox.Show("赛程信息输入不完整，请完善");
                return;
            }
            else if (comboBox1.Text==comboBox2.Text)
            {
                MessageBox.Show("主队和客队不能是同一个队伍");
                return;
            }
            else
            {
                int zhu=1;
                int ke=1;
                if (comboBox1.Text== "暴龙战士队")
                {
                    zhu = 1;
                }
                else if (comboBox1.Text == "卧龙凤雏队")
                {
                    zhu = 2;
                }
                else if (comboBox1.Text == "勇敢牛牛队")
                {
                    zhu = 3;
                }
                else if (comboBox1.Text == "精神小伙队")
                {
                    zhu = 4;
                }
                else if (comboBox1.Text == "乒乒乓乓队")
                {
                    zhu = 5;
                }

                if (comboBox2.Text == "暴龙战士队")
                {
                    ke = 1;
                }
                else if (comboBox2.Text == "卧龙凤雏队")
                {
                    ke = 2;
                }
                else if (comboBox2.Text == "勇敢牛牛队")
                {
                    ke = 3;
                }
                else if (comboBox2.Text == "精神小伙队")
                {
                    ke = 4;
                }
                else if (comboBox2.Text == "乒乒乓乓队")
                {
                    ke = 5;
                }

                dBHelper.Openconnection();
                string sql = "insert ScheduleInfo(count,hostTeamId,awayTeamId,competitionTime,competitionPosition,score,entryPerson) values('" + textBox1.Text + "', '" + zhu + "', '" + ke + "', '" + textBox2.Text + "', '" + textBox4.Text + "', '" + textBox3.Text + "', '"+textBox5.Text+"')";
                SqlCommand command = new SqlCommand(sql, dBHelper.Connection);
                command.ExecuteNonQuery();
                MessageBox.Show("添加成功");
                clear();
            }
            
        }

        private void clear()
        {
            this.textBox1.Text = null;
            this.textBox2.Text = null;
            this.textBox3.Text = null;
            this.textBox4.Text = null;
            this.textBox5.Text = null;
            this.comboBox1.Text = "暴龙战士队";
            this.comboBox2.Text = "暴龙战士队";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            clear();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
